﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class ReceiveCfgForm : Form
    {
        public ReceiveCfgForm()
        {
            InitializeComponent();
        }

        private bool m_IsNew = true;
        public bool IsNew
        {
            get { return m_IsNew; }
            set { m_IsNew = value; }
        }

        private string m_Code = "";
        public string Code
        {
            get { return m_Code; }
            set { m_Code = value; }
        }

        private string m_Receive = "";
        public string Receive
        {
            get { return m_Receive; }
            set { m_Receive = value; }
        }


        private void ReceiveCfgForm_Load(object sender, EventArgs e)
        {
            if (m_IsNew)
            {
                txtCode.Text = "";
                txtCode.Enabled = true;
                txtReceive.Text = "";
                this.Text = "添加收货单位";
            }
            else
            {
                txtCode.Text = m_Code;
                txtCode.Enabled = false;
                txtReceive.Text = m_Receive;
                this.Text = "修改收货单位";
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                MessageBox.Show("请输入编号！");
                return;
            }
            if (string.IsNullOrEmpty(txtReceive.Text.Trim()))
            {
                MessageBox.Show("请输入收货单位！");
                return;
            }
            m_Code = txtCode.Text.Trim();
            m_Receive = txtReceive.Text.Trim();

            string sqlStr = "";
            DataTable dt = new DataTable();
            if (m_IsNew)
            {
                //判断是否存在相同的编号
                sqlStr = "select * from Receive where Code = '" + m_Code + "'";
                dt = CommonHelper.DoSelectOpreation(sqlStr);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("已存在相同的编号");
                    return;
                }
            }
            //判断是否存在相同的物料
            if (m_IsNew)
                sqlStr = "select * from Receive where Name = '" + m_Receive + "'";
            else
                sqlStr = "select * from Receive where Name = '" + m_Receive + "' and Code <> '" + m_Code + "'";
            dt = CommonHelper.DoSelectOpreation(sqlStr);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("已存在相同的收货单位名称");
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
